package com.hxxy.wyp.petsLover.common.services.base.backend.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxxy.wyp.petsLover.common.entities.*;
import com.hxxy.wyp.petsLover.common.entities.vo.req.DashboardOrderVo;
import com.hxxy.wyp.petsLover.common.entities.vo.req.GoodSumByCategory;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.DashboardVo;
import com.hxxy.wyp.petsLover.common.entities.vo.resp.Top5Vo;
import com.hxxy.wyp.petsLover.common.mapper.*;
import com.hxxy.wyp.petsLover.common.services.base.backend.DashboardService;
import com.hxxy.wyp.petsLover.exception.AppException;
import net.sf.jsqlparser.statement.select.Top;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DashboardServiceImpl
 * @Description TODO
 * @Author wyp
 * @Date 2022/12/25 11:17
 * @Version 1.0
 */
@Service
public class DashboardServiceImpl implements DashboardService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GoodMapper goodMapper;
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private UserShareMapper userShareMapper;
    @Resource
    private ShopCartMapper shopCartMapper;

    @Override
    public DashboardVo countAll() {
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getOrderStatus, "1");
        List<Orders> orders = ordersMapper.selectList(lambdaQueryWrapper);
        TreeMap<String, Long> stringIntegerTreeMap = new TreeMap<>();
        ArrayList<Top5Vo> top5Vos = new ArrayList<>();
        orders.stream().map(orders1 -> {
            List<String> collect = Arrays.stream(orders1.getShopCartId().split(",")).collect(Collectors.toList());
            collect.stream().map(s -> {
                ShopCart shopCart = shopCartMapper.selectById(s);
                stringIntegerTreeMap.merge(shopCart.getCommodityName(), shopCart.getCommodityNumber(), Long::sum);
                return s;
            }).collect(Collectors.toList());
            return orders1;
        }).collect(Collectors.toList());
        for (String key : stringIntegerTreeMap.keySet()) {
            Top5Vo top5Vo = new Top5Vo();
            top5Vo.setName(key);
            top5Vo.setValue(stringIntegerTreeMap.get(key));
            top5Vos.add(top5Vo);
        }
        Collections.sort(top5Vos, new Comparator<Top5Vo>() {
            @Override
            public int compare(Top5Vo o1, Top5Vo o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        //
        DashboardVo dashboardVo = new DashboardVo();
        DashboardVo.GoodSum goodSum = new DashboardVo.GoodSum();
        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Good> goodQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Message> messageQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Orders> ordersQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<UserShare> userShareQueryWrapper = new LambdaQueryWrapper<>();
        Long userNumber = userMapper.selectCount(userQueryWrapper);
        Long commoditiesNumber = goodMapper.selectCount(goodQueryWrapper);
        Long messagesNumber = messageMapper.selectCount(messageQueryWrapper);
        Long ordersNumber = ordersMapper.selectCount(ordersQueryWrapper);
        Long shareNumber = userShareMapper.selectCount(userShareQueryWrapper);
        List<DashboardVo> dashboardVoList = ordersMapper.sumPriceGroupByDate();
        List<DashboardOrderVo> dashboardOrderVos = ordersMapper.sumOrderNums();
        Integer integer = ordersMapper.sumOrderMoneyByDay(new LocalDate().toString());
        List<GoodSumByCategory> goodSumByCategories = goodMapper.goodsumByCategory();

        List<String> str1 = new ArrayList<>();
        List<Integer> str2 = new ArrayList<>();
        List<Integer> orderNum = new ArrayList<>();
        List<String> goodNames = new ArrayList<>();
        List<Integer> numbers = new ArrayList<>();
        dashboardVoList.forEach(dashboardVo1 -> {
            dashboardVo1.getGoodSums().forEach(goodSum1 -> {
                str1.add(goodSum1.getXData());
                str2.add(goodSum1.getYData());
            });
        });
        dashboardOrderVos.forEach(dashboardOrderVo -> {
            orderNum.add(dashboardOrderVo.getYxData());
        });
        goodSumByCategories.forEach(goodSumByCategory -> {
            goodNames.add(goodSumByCategory.getGoodName());
            numbers.add(goodSumByCategory.getNumber());
        });
        goodSum.setXxData(str1);
        goodSum.setYyData(str2);
        dashboardVo.setGoodSum(goodSum);
        dashboardVo.setUserNumber(userNumber);
        dashboardVo.setCommoditiesNumber(commoditiesNumber);
        dashboardVo.setMessagesNumber(messagesNumber);
        dashboardVo.setOrdersNumber(ordersNumber);
        dashboardVo.setShareNumber(shareNumber);
        dashboardVo.setOrderSumByDate(orderNum);
        dashboardVo.setTop5Vos(top5Vos);
        if (integer == null) {
            dashboardVo.setIncomeToday("0");
        } else {
            dashboardVo.setIncomeToday(integer.toString());
        }
        dashboardVo.setGoodNames(goodNames);
        dashboardVo.setNumbers(numbers);
        return dashboardVo;

        //TODO
    }
}
